#!/bin/sh
''':'
experiments=${GIT_CINNABAR_EXPERIMENTS-$(git config cinnabar.experiments)}
case ",$experiments," in
*,python3,*)
  PYTHON=python3
  ;;
*)
  PYTHON=python2.7
  ;;
esac
exec $PYTHON $0 "$@"
exit 1
'''

from __future__ import division
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__)))
sys.path.append(os.path.join(os.path.dirname(__file__), 'pythonlib'))

from cinnabar.githg import GitHgStore
from cinnabar.hg.repo import Remote
from cinnabar.remote_helper import (
    GitRemoteHelper,
    TagsRemoteHelper,
)
from cinnabar.util import (
    fsencode,
    run,
)


def main(args):
    if sys.platform == 'win32':
        # By default, sys.stdout on Windows will transform \n into \r\n, which
        # the calling git process won't recognize in our answers.
        import msvcrt
        msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    assert len(args) == 2
    remote = Remote(*(fsencode(a) for a in args))

    store = GitHgStore()

    if remote.url == 'tags:':
        helper = TagsRemoteHelper(store)
    else:
        helper = GitRemoteHelper(store, remote)
    helper.run()

    store.close()


if __name__ == '__main__':
    run(main, sys.argv[1:])
